package com.xian.im.modules.im.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xian.im.common.model.from.DictFrom;
import com.xian.im.common.utils.sys.PageUtils;
import com.xian.im.modules.im.dao.DictDao;
import com.xian.im.modules.im.entity.DictEntity;
import com.xian.im.modules.im.service.DictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class DictServiceImpl extends ServiceImpl<DictDao, DictEntity> implements DictService {


    /**
     * @param from
     * @return
     */
    @Override
    public PageUtils<DictDao> findPage(DictFrom from) {
        String type = from.getType();
        String code = from.getCode();
        if(from.getCurrPage() ==null){
            from.setCurrPage(1);
        }
        if(from.getPageSize() == null){
            from.setPageSize(10);
        }
        IPage<DictEntity> page = new Page(from.getCurrPage(),from.getPageSize());
        IPage <DictEntity>iPage = this.lambdaQuery().select(DictEntity::getCode,
                DictEntity::getType,
                DictEntity::getOrderNum,
                DictEntity::getName,
                DictEntity::getRemark,
                DictEntity::getValue).eq(StringUtils.isNotBlank(type), DictEntity::getType, type)
                .eq(StringUtils.isNotBlank(code), DictEntity::getCode, code)
                .page(page);
        List<DictEntity> dictList = page.getRecords();
        List<DictDao> dtoList = new ArrayList<>();
        for (DictEntity dictEntity : dictList) {
            dtoList.add(BeanUtil.copyProperties(dictEntity,DictDao.class));
        }
        PageUtils<DictDao> pageUtils = new PageUtils<DictDao>(dtoList,iPage);
        return pageUtils;
    }
}
